package com.wanxi.product.core_sevice.impl;

import com.wanxi.product.mapper.PriceMapper;
import com.wanxi.product.service.api.dto.PriceDto;
import com.wanxi.product.service.api.dto.SkuDto;
import com.wanxi.product.service.api.entity.Price;
import com.wanxi.product.service.api.entity.Sku;
import com.wanxi.product.service.api.dto.SkuQueryDto;
import com.wanxi.product.core_sevice.SkuService;
import com.wanxi.product.mapper.SkuMapper;
import com.wanxi.product.util.BeanCopyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SkuServiceImpl implements SkuService {
    @Resource
    SkuMapper skuMapper;

    @Override
    public Long addSku(SkuDto skuDto) {
        Sku sku = BeanCopyUtils.copy(skuDto, Sku.class);
        skuMapper.add(sku);
        return sku.getSkuId();
    }

    @Override
    public List<SkuDto> findAllSku(SkuQueryDto skuQueryDto) {
        List<Sku> skuList = skuMapper.findAll(skuQueryDto);
        return BeanCopyUtils.copyList(skuList, SkuDto.class);
    }

    @Override
    public SkuDto findSkuBySkuId(Long skuId) {
        Sku sku = skuMapper.findById(skuId);
        return BeanCopyUtils.copy(sku, SkuDto.class);
    }

    @Override
    public Long updateSku(SkuDto skuDto) {
        Sku sku = BeanCopyUtils.copy(skuDto, Sku.class);
        skuMapper.update(sku);
        return sku.getSkuId();
    }

    @Override
    public int deleteSku(Long skuId) {
        return skuMapper.delete(skuId);
    }

    @Resource
    PriceMapper priceMapper;

    @Override
    public Long addPrice(PriceDto priceDto) {
        Price price = BeanCopyUtils.copy(priceDto, Price.class);
        priceMapper.add(price);
        return price.getSkuId();
    }

    @Override
    public PriceDto findPriceBySkuId(Long skuId) {
        Price price = priceMapper.findBySkuId(skuId);
        return BeanCopyUtils.copy(price, PriceDto.class);
    }

    @Override
    public Long updatePrice(PriceDto priceDto) {
        Price price = BeanCopyUtils.copy(priceDto, Price.class);
        priceMapper.update(price);
        return price.getSkuId();
    }

    @Override
    public int deletePrice(Long skuId) {
        return priceMapper.delete(skuId);
    }

}
